var utils    = require('../../../utils/utils.js');
var amapFile = require('../../../utils/amap-wx.js');
var latitude, longitude;
var PX = 0.0056, PY = -0.00228;
var map_origin, map_destination;
var list = [], map_markers = [], points = [];
var myAmapFun = new amapFile.AMapWX({ key: "2860c828021d2e948673517214ac6b1b" });
var ori = "", des = "";

Page({
  
  data: {
    loc: [], 
    markers: [],
    distance: '',
    cost: '',
    polyline: [],
  },

  //打开地图，并显示位置
  mapLoc: e => {
    console.log(e);
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        latitude = Number(list[e.currentTarget.id].lat);
        longitude = Number(list[e.currentTarget.id].lon);
        wx.openLocation({
          latitude,
          longitude,
          scale: 18,
        });
      }
    })
  },

  onShow:function(){
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        that.setData({ now_la: res.latitude, now_lo: res.longitude });
      },
    });
    wx.req('attendanceTaskInfoGet', { user_id: wx.appData.user_id }, '刷新位置中').then((res) => {
      wx.req('attendanceHistoryGet', { user_id: wx.appData.user_id }).then(history => {
        console.log(res.odr, res.data)
        var r = res.data.data, time = '';
        list = [];
        for (var i = 0; i < r.length; i++) {
          for (var j = 0; j < wx.appData.loc.length; j++) {
            if (r[i].location_id == wx.appData.loc[j].location_id) {
              // var timeString = utils.catchTime({
              //   hour: r[i].time.h,
              //   minute: r[i].time.m,
              //   second: r[i].time.s,
              // });
              console.log(r[i], wx.appData.loc[j])
              map_markers.push({
                iconPath: '../../img/mapicon_navi_s.png',
                id: i,
                title: wx.appData.loc[j].name,
                latitude: wx.appData.loc[j].la + PY,
                longitude: wx.appData.loc[j].lo + PX,
                callout: {
                  content: wx.appData.loc[j].name + '\n' + r[i].start_time,
                  borderRadius: 10,
                  padding: 10,
                  borderWidth: 1,
                  borderColor: '#66d1ff',
                  display: 'BYCLICK',
                  textAlign: 'center',
                },
              });
              list.push({
                id: i + 1,
                info: r[i].task_info_id,
                name: wx.appData.loc[j].name,
                lat: wx.appData.loc[j].la,
                lon: wx.appData.loc[j].lo,
                time: r[i].start_time,
                opacity: 1,
                show: 0,
              });
              break;
            }
          }
        }
        console.log(list)
        for (let i = 0; i < history.data.data.length; i++) {
          for (let j = 0; j < list.length; j++) {
            console.error(history.data.data[i], list[j])
            if (history.data.data[i].task_info_id == list[j].info) {
              list[j].opacity = 0.4;
              list[j].show = 1;
            }
          }
        }
        that.setData({
          loc: list,
          markers: map_markers,
        });
        console.log(that.data.loc);
        if (that.data.loc.length == 1) return 0;
        // 刷新完成，获取路径点
        wx.showLoading({ title: '绘制路线中' });
        ori = that.data.loc[0].lon + ',' + that.data.loc[0].lat;
        //that.getLine(that.data.loc.length - 1);
        for (var i = 0; i < that.data.loc.length; i++) {
          points.push({
            longitude: parseFloat(that.data.loc[i].lon) + PX,
            latitude: parseFloat(that.data.loc[i].lat) + PY,
          });
        }
        that.setData({
          polyline: [{
            points: points,
            color: "#0091ff",
            width: 2,
          }],
        });
        wx.hideLoading();
      })
    })
  },

  markerTap: function (e) {
    console.log(e);
  },

  debugTap: function (e) {
    console.log(e);
  },


  getLine: function (n) {
    var that = this, tmp = that.data.loc;
    //des = parseFloat(tmp[tmp.length - n].lon + 0.3457283) + ',' + parseFloat(tmp[tmp.length - n].lat + 0.001915);
    des = tmp[tmp.length - n].lon + ',' + tmp[tmp.length - n].lat;
    myAmapFun.getDrivingRoute({
      origin: ori, 
      destination: des,
      success: function (data) {
        console.log(data);
        if (data.paths && data.paths[0] && data.paths[0].steps) {
          var steps = data.paths[0].steps;
          for (var i = 0; i < steps.length; i++) {
            var poLen = steps[i].polyline.split(';');
            for (var j = 0; j < poLen.length; j++) {
              points.push({
                longitude: parseFloat(poLen[j].split(',')[0]) + PX,
                latitude: parseFloat(poLen[j].split(',')[1]) + PY,
              });
            }
          }
        }
        ori = tmp[tmp.length - n].lon + ',' + tmp[tmp.length - n].lat;
        if (n == 1) {
          that.setData({
            polyline: [{
              points: points,
              color: "#0091ff",
              width: 6,
            }],
            markers: map_markers,
          });
          wx.hideLoading();
          console.log("successful, hhhhhhh");
          return 0;
        }
        else return that.getLine(n - 1);
      },
      fail: function (info) { }
    })
   
  },


})